package com.example.administrator.yuejian23.Fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian23.Activity.AboutYuejianActivity;
import com.example.administrator.yuejian23.Activity.LoginActivity;
import com.example.administrator.yuejian23.Activity.MyGuanZhuActivity;
import com.example.administrator.yuejian23.Activity.MyOrderActivity;
import com.example.administrator.yuejian23.Activity.MyShaiTuActivity;
import com.example.administrator.yuejian23.Activity.OrderActivity;
import com.example.administrator.yuejian23.Activity.SettingActivity;
import com.example.administrator.yuejian23.Bean.MyBean;
import com.example.administrator.yuejian23.Bean.UserInfo;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.MainActivity;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {

    private TextView user_name;
    private TextView user_shaitunumber;
    private TextView user_dingdannumber;
    private TextView user_guanzhunumber;
    private ImageView mypic;
    private TextView setting_button;
    private TextView Aboutyuejian_button;
    private TextView Exit_button;
  /*  private int userid;*/
  /*  private RecyclerView recyclerView;
    private MyAdapter adapter;*/
    private MyBean myList;
    private int userid;
    private UserInfo userInfo;

    public MyFragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_myinfo, container, false);
        user_name = (TextView) view.findViewById(R.id.me_name);
        user_shaitunumber = (TextView) view.findViewById(R.id.me_shaitu);
        user_dingdannumber = (TextView) view.findViewById(R.id.me_dingdan);
        user_guanzhunumber = (TextView) view.findViewById(R.id.me_guanzhu);
        mypic = (ImageView) view.findViewById(R.id.mypic);
        setting_button = (TextView) view.findViewById(R.id.textView24);
        Aboutyuejian_button= (TextView) view.findViewById(R.id.textView25);
        Exit_button= (TextView) view.findViewById(R.id.exit_button);
       /* final String userid = "97";*/
        userid = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        Log.v("获取id++++++++++", String.valueOf(userid));
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service = retrofit.create(RetrofitService.class);
        retrofit2.Call<MyBean> call = service.MyList(userid);
        call.enqueue(new Callback<MyBean>() {
            @Override
            public void onResponse(Call<MyBean> call, Response<MyBean> response) {
                myList = response.body();
                user_name.setText(myList.getUser_name());
                user_shaitunumber.setText(myList.getShaitunum());
                user_dingdannumber.setText(myList.getFocusnum());
                user_guanzhunumber.setText(myList.getMessagenum());
             /*   Picasso.with(getContext()).load(Const.PIC_URL + myList.getPhoto()).into(mypic);*/

                if (myList.getPhoto()==null){
                    Picasso.with(getContext())
                            .load(Const.PIC_URL + myList.getPhoto())
                            .placeholder(R.mipmap.ic_myphoto)
                            .noFade()
                            .into(mypic);
                }
                else {
                    Picasso.with(getContext()).load(Const.PIC_URL + myList.getPhoto()).into(mypic);
                }
            }

            @Override
            public void onFailure(Call<MyBean> call, Throwable t) {

            }
        });
       user_shaitunumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyShaiTuActivity.class);
                intent.putExtra("User_Id", userid);
                intent.putExtra("User_Name", myList.getUser_name());
                intent.putExtra("User_Pic", Const.PIC_URL + myList.getPhoto());
                startActivity(intent);
            }
        });
        Aboutyuejian_button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             Intent intent=new Intent(getContext(), AboutYuejianActivity.class);
             getContext().startActivity(intent);}
        });
         user_guanzhunumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyGuanZhuActivity.class);
                intent.putExtra("User_Id", userid);
                startActivity(intent);
            }
        });
        user_dingdannumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyOrderActivity.class);
                intent.putExtra("User_Id", userid);
                getContext().startActivity(intent);
            }
        });
        setting_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SettingActivity.class);
               /* intent.putExtra("User_Id", userid);*/
                getContext().startActivity(intent);
            }
        });
        Exit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),LoginActivity.class);
                Toast.makeText(getActivity(), "退出成功", Toast.LENGTH_SHORT).show();
                getContext().startActivity(intent);}
        });


        return view;
    }
}
/*  @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     *//*   recyclerView= (RecyclerView) view.findViewById(R.id.recycleMy);*//*
          recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView);
          Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create()).build();
        RetrofitService retrofitService =retrofit.create(RetrofitService.class);
        Call<MyBean> call =retrofitService.MyList(getUserId());
        call.enqueue(new Callback<MyBean>() {
            @Override
            public void onResponse(Call<MyBean> call, Response<MyBean> response) {
                myList=response.body();
                adapter=new MyAdapter(getActivity(),myList);
                LinearLayoutManager layoutManager =new LinearLayoutManager(getActivity());
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<MyBean> call, Throwable t) {

            }
        });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_setting, container, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
        View view= inflater.inflate(R.layout.fragment_setting, container, false);
    }
}*/
